<?php
require_once("thamdinh/models/linhvuc.php");
class Danhmuc_LinhvucController extends GenericController {

    public function indexAction()
    {
        $this->_helper->layout->disableLayout();
        $params=$this->getRequest()->getParams();
        $data= LinhVuc::SelectLinhVucListFull("");
        $config = Zend_Registry::get('config');
        $paginatorLimit=$config->paginatorLimit;
        $paginatorRange=$config->paginatorRange;
        $paginator = Zend_Paginator::factory($data);
        $paginator->setItemCountPerPage($paginatorLimit);       
        $paginator->setPageRange($paginatorRange);
        $currentPage = $this->getRequest()->getParam('page',1);
        $paginator->setCurrentPageNumber($currentPage);        
        $this->view->data=$paginator;
        $this->view->limit=$paginatorLimit;
        $this->view->page=$currentPage;
        if($params['paginator']!=null)
        {
            $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer('linhvuc/index', null, true);
        }
    }
    
    public function saveAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $param=$this->getRequest()->getParams();
        $id=$param['id'];
        $dataArray=array(
            'NAME'=>  trim($param['NAME']),
            'ACTIVE'=> (int) $param['ACTIVE']                
        );
        $linhvucModel=new LinhVuc();
        if($id && (int)$id>0)
        {
            //UPDATE ACTION
            $linhvucModel->update($dataArray,'ID_LV='.$id);
        }
        ELSE
        {
            //SAVE ACTION            
            $linhvucModel->insert($dataArray);
        }
        $this->_redirect('/danhmuc/linhvuc/index');
    }
    
    public function inputAction()
    {
        $this->_helper->layout->disableLayout();
        $id=$this->getRequest()->getParam('id');
        if($id && (int)$id>0)
        {
            $this->view->data= LinhVuc::SelectLinhVucById($id);
        }        
    }
    
    public function deleteAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $ids=$this->getRequest()->getParam('ids');
        $model = new LinhVuc();
        foreach($ids as $id)
        {    
            for($i=0;$i<count($id);$i++)
            {
                $model->delete('ID_LV='.$id[$i]);
            }  
            
        }        
        $this->_redirect('/danhmuc/linhvuc/index');
    }
}
